---@diagnostic disable: undefined-global
---
--- Created by 15797.
--- DateTime: 2025/9/18 上午11:03
---
local userid = ARGV[1];
local voucherid = ARGV[2];
local stock_key = "seckill:stock:" .. voucherid;
local order_key = "seckill:order:" .. voucherid;

-- 检查库存
local stock = redis.call("GET", stock_key)

if not stock then
    return -3
end
if tonumber(stock) <= 0 then
    return -1
end

if redis.call("SISMEMBER", order_key, userid) == 1 then
    return -2
end

redis.call("DECR", stock_key)        --减库存
redis.call("SADD", order_key, userid) --sadd 向集合中添加元素
redis.call("EXPIRE", order_key, 50000) --设置过期时间
redis.call("XADD", 'orderstream', '*', 'userid', userid, 'voucherid', voucherid)

return 0
